package com.tangruitao.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tangruitao.bean.*;
import com.tangruitao.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 这个类的作者是13536
 * 开发日期:2021/12/20
 **/
@Controller
public class GoodsController {
    @Autowired
    GoodsService goodsService;
    @RequestMapping("tologin")
    public String tologin(HttpServletRequest request){
        request.setAttribute("users",new Users());
        return "login";
    }
    @RequestMapping("login")
    public String login(HttpServletRequest request, @Validated @ModelAttribute("users") Users users, BindingResult result){
        Users login = goodsService.login(users.getUname());
        if(login==null){
            result.rejectValue("uname","","账号名错误");
            return "login";
        }
        if(login!=null) {
            if (!login.getUpwd().equals(users.getUpwd())) {
                result.rejectValue("upwd", "", "密码错误");
                return "login";
            } else {
                request.getSession().setAttribute("user", login);
            }
        }
        if(result.hasErrors()){
            return "login";
        }
        return "redirect:list";
    }

    @RequestMapping("list")
    public String list(HttpServletRequest request, QureyOv qureyOv){
        if(qureyOv.getCur()==null || qureyOv.getCur().equals("")){
            qureyOv.setCur("1");
        }
        PageHelper.startPage(Integer.parseInt(qureyOv.getCur()),2);
        List<Goods> listgoods = goodsService.listgoods(qureyOv);
        PageInfo<Goods> pp=new PageInfo<>(listgoods);
       Users user = (Users) request.getSession().getAttribute("user");
       if(user!=null){
           qureyOv.setUid(user.getUid());
       }
        request.setAttribute("list",listgoods);
        request.setAttribute("pp",pp);
        request.setAttribute("ov",qureyOv);
        return "list";
    }


    @RequestMapping("toadd")
    public String toadd(){
        return "add";
    }
    @RequestMapping("add")
    public String add(Goods goods){
        Integer i = goodsService.insertgood(goods);
        if(i>0){
            return "redirect:list";
        }
        return "add";
    }


    @RequestMapping("photo")
    @ResponseBody
    public String photo(MultipartFile file) throws IOException {
       if(file!=null){
           String origin = file.getOriginalFilename();
           int i = origin.lastIndexOf(".");
           String sub = origin.substring(i);
           String s = UUID.randomUUID().toString();
           String newfilename=s+sub;
           file.transferTo(new File("d:/pic/",newfilename));
           return newfilename;
       }
       return "";
    }

    @ResponseBody
    @RequestMapping("typelist")
    public List<Type>  typelist(){
       return goodsService.typelist();
    }

    @ResponseBody
    @RequestMapping("brandlist")
    public  List<Brand> brandlist(){
       return goodsService.brandlist();

    }

}
